﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using Microsoft.Windows.Controls;
using System.Threading;
using Nova.dataDataSetTableAdapters;
using System.Reflection;

namespace Nova
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainForm : Window
    {

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Window owner = Window.GetWindow(this);
            AddForm addForm = new AddForm(this);
            addForm.ShowInTaskbar = false;
            addForm.Owner = this;
            addForm.ShowDialog();
        }

        private void btnQuit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void RefreshList()
        {
           BudgetDataProvider pr = new BudgetDataProvider();
            dataGrid1.DataContext = pr.GetBudget();
            string searchText = tbFilter.Text;
            filter(dataGrid1, searchText);
            if (dataGrid1.Items.Count > 0)
                dataGrid1.SelectedIndex = 0;
        }

        public void filter(DataGrid dataGridView, string filterValue) {
            DataView dv = dataGridView.DataContext as DataView;
            string filterStr = string.Empty;
            double filterD;
            Nova.dataDataSet.BudgetViewDataTable table = new dataDataSet.BudgetViewDataTable();
            if (double.TryParse(filterValue, out filterD)) {
                 foreach (DataColumn pi in table.Columns){

                     if (pi.DataType == typeof(Decimal)) {
                        filterStr += pi.ColumnName;
                        filterStr += " = " + ((filterValue == "") ? "0" : filterValue) + " ";
                        filterStr += " or ";
                    }
                     if (pi.DataType == typeof(Int32)) {
                         filterStr += pi.ColumnName;
                        filterStr += " = " + ((filterValue == "") ? "0" : filterValue) + " ";
                        filterStr += " or ";
                    }
                     if (pi.DataType == typeof(float)) {
                         filterStr += pi.ColumnName;
                        filterStr += " = " + ((filterValue == "") ? "0" : filterValue) + " ";
                        filterStr += " or ";
                    }
                     if (pi.DataType == typeof(Nullable<Int32>)) {
                         filterStr += pi.ColumnName;
                        filterStr += " = " + ((filterValue == "") ? "0" : filterValue) + " ";
                        filterStr += " or ";
                    }
                     if (pi.DataType == typeof(Nullable<Decimal>)) {
                         filterStr += pi.ColumnName;
                        filterStr += " = " + ((filterValue == "") ? "0" : filterValue) + " ";
                        filterStr += " or ";
                    }
                     if (pi.DataType == typeof(Nullable<float>)) {
                         filterStr += pi.ColumnName;
                        filterStr += " = " + ((filterValue == "") ? "0" : filterValue) + " ";
                        filterStr += " or ";
                    }
                     if (pi.DataType == typeof(string)) {
                         filterStr += pi.ColumnName;
                        filterStr += " like '%" + filterValue + "%' ";
                        filterStr += " or ";
                    }
                }
            } else {                                
                foreach (DataColumn pi in table.Columns) {

                    if (pi.DataType == typeof(string)) {
                        filterStr += pi.ColumnName;
                        filterStr += " like '%" + filterValue + "%' ";
                        filterStr += " or ";
                    }

                }
            }
            filterStr = filterStr.Remove(filterStr.LastIndexOf("or"));
            dv.RowFilter = filterStr;
            dataGridView.DataContext = dv;
        }

       
        private void Edit()
        {
            if (dataGrid1.SelectedIndex != -1)
            {
                Window owner = Window.GetWindow(this);
                DataRowView dr = dataGrid1.SelectedItem as DataRowView;
                AddForm addForm = new AddForm(this, Convert.ToInt32(dr.Row["id"]));
                addForm.ShowInTaskbar = false;
                addForm.Owner = this;
                addForm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Выберите запись!");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.Items.Count>0)
                dataGrid1.SelectedIndex = 0;
        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DependencyObject source = (DependencyObject)e.OriginalSource;
            var row = IUHelper.TryFindParent<DataGridRow>(source);

            //the user did not click on a row
            if (row == null) return;

            Edit();

            e.Handled = true;

           
        }

        private void btnMod_Click(object sender, RoutedEventArgs e)
        {
            Edit();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedIndex != -1)
            {
                MessageBoxResult res = MessageBox.Show("Уверены что хотите удалить запись без возможности восстановления?", "Удаление:", MessageBoxButton.YesNo);
                if (res == MessageBoxResult.Yes)
                {
                    DataRowView dr = dataGrid1.SelectedItem as DataRowView;
                    int id = Convert.ToInt32(dr.Row["id"]);
                    BudgetTableAdapter da = new BudgetTableAdapter();
                    da.Delete(id);
                    RefreshList();
                    status.Content = "Запись удалена";
                }
            }
            else {
                MessageBox.Show("Выберите запись!");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e) {
            tbFilter.Text = "";
            string searchText = tbFilter.Text;
            filter(dataGrid1, searchText);
        }

        private void tbFilter_KeyUp(object sender, KeyEventArgs e) {
            string searchText = tbFilter.Text;
            filter(dataGrid1, searchText);
        }
    }
}
